package com.lhf.dajiuye.controller;

import com.github.pagehelper.PageInfo;
import com.lhf.dajiuye.aspect.OpenIdHandle;
import com.lhf.dajiuye.domain.*;
import com.lhf.dajiuye.service.UpLoadImgService;
import com.lhf.dajiuye.service.UserService;
import com.lhf.dajiuye.service.WxService;
import com.lhf.dajiuye.utils.EncryptUtil;
import com.lhf.dajiuye.utils.FileNameUtil;
import com.lhf.dajiuye.utils.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    UpLoadImgService upLoadImgService;

    @Autowired
    UserService userService;

    @Autowired
    WxService wxService;

    /**
     * 通过fromOpenId,toOpenId,news保存用户消息
     * @param msg
     * @return
     */
    @PostMapping(value = "/saveMessage")
    public String saveMessage(@RequestBody Message msg){
        System.out.println(msg);
        userService.saveMessage(msg);
        return "保存信息成功";
    }

    /**
     * 通过fromOpenId,userId,news保存用户消息
     * @param msg
     * @return
     */
    @PostMapping(value = "/saveMessage2")
    public String saveMessage2(@RequestBody Message msg){
        System.out.println(msg);
        msg.setFromOpenId(EncryptUtil.decryptUtil(msg.getFromOpenId())); // 解谜出fromOpenId
        User user = userService.getUserById(String.valueOf(msg.getToOpenId()));
        msg.setToOpenId(user.getOpenId()); // 将id转为对应openId
        userService.saveMessage(msg);
        return "保存信息成功";
    }

    /**
     * 查询某个openId对应的所有消息
     * @param openId
     * @return
     */
    @GetMapping(value = "/getAllMessage")
    @OpenIdHandle
    public CommonResult2 getAllMessage(@RequestParam("openId") String openId){
        log.info("getAllMessage的openId为{}==============",openId);
        List<MessageDetail> messages = userService.getAllMessageDetail(openId);
        // 这个列表只是查询到的所有信息，还得把同一个人的信息放在一起
        return new CommonResult2<List>(MessageUtil.mergeMsg(messages,openId),new Meta("获取成功",200));
    }

    /**
     * 获取openId对应的用户的所有信息
     * @param openId
     * @return
     */
    @GetMapping("/getUser")
    @OpenIdHandle
    public Object getUser(@RequestParam("openId") String openId){
        log.info("进来getuser,openId={}========",openId);
        User user = userService.getUserByOpenId(openId);
        return new CommonResult2<User>(user,new Meta("获取成功",200));
    }

    /**
     * 获取所有的投递者信息
     * @param params2
     * @return
     */
    @GetMapping("/getusers")
    public Object TGetUsers(Params2 params2){
        log.info("进来getuser,openId={}========",params2);
        PageInfo<User> users = userService.getUsers(params2);
        return new CommonResult2<PageInfo>(users,new Meta("获取成功",200));
    }

    /**
     * 返回用户解密后的个人信息"nickName":"moon","gender":0,"language":"zh_CN","city":""。。。
     * @param code
     * @param encryptedData
     * @param iv
     * @return
     */
//    @GetMapping("/handleWXMsg")
//    @ResponseBody
//    public Object handleWXMsg(@RequestParam(value = "code") String code,
//                           @RequestParam(value = "encryptedData") String encryptedData,
//                           @RequestParam(value ="iv") String iv){
//        Map msg=wxService.getUserInfo(code,encryptedData, iv);
//        log.info("msg:============={}",msg);
//        return new CommonResult3(msg,new Meta("获取成功",200));
//    }

    /**
     * 返回用户解密后的个人信息"nickName":"moon","gender":0,"language":"zh_CN","city":""。。。
     * @param

     * @return
     */
    @PostMapping("/handleWXMsg")
    @ResponseBody
    public Object handleWXMsg( LoginInfo loginInfo){
        Map msg=wxService.getUserInfo(loginInfo);
        log.info("msg:============={}",msg);
        return new CommonResult3(msg,new Meta("获取成功",200));
    }

    /**
     * 用户发送简历
     * @param userdeliver
     * @return
     */
    @GetMapping(value = "/saveDeliver")
    public String saveDeliver(@ModelAttribute Userdeliver userdeliver){
//        System.out.println(msg);
        userService.saveDeliver(userdeliver);
        return "保存信息成功";
    }
    /**
     * 更新简历投递状态
     * @param userdeliver
     * @return
     */
    @GetMapping(value = "/updateDeliver")
    public String updateDeliver(@ModelAttribute Userdeliver userdeliver){
//        System.out.println(msg);
        userService.updateDeliver(userdeliver);
        return "保存信息成功";
    }
}
